package com.nhc.database.manager;

import android.database.SQLException;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteException;

import com.astrob.activitys.AppContext;
import com.nhc.database.dbmodel.QiyaInfo;
import com.nhc.database.greendao.QiyaInfoDao;

import java.util.ArrayList;


public class QiyaCenter {

    private static QiyaCenter instance = new QiyaCenter();

    public static QiyaCenter getInstance() {
        return instance;
    }

    public void init() {

    }

    //气压信息列表
    private ArrayList<QiyaInfo> qiyaInfoList = new ArrayList<QiyaInfo>();

    private boolean _isClearHis = false;//开机后清理一次记录

    //气压信息
    public void doQueryQiyaInfo() {
        //查询5天内的数据
        QiyaInfoDao qiyaInfoDao = AppContext.getInstances().getDaoSession().getQiyaInfoDao();

        long thisday = (System.currentTimeMillis() / 86400000) * 86400000;
        thisday -= 86400000 * 5;

        if (!_isClearHis) {
            //删除5天前的数据
            _isClearHis = true;
            try {
                String deletesql = "DELETE FROM QIYA_INFO WHERE DATETIME < " + thisday;
                qiyaInfoDao.getDatabase().execSQL(deletesql);
            } catch (SQLException e) {
                return;
            }
        }

        try {
            qiyaInfoList = (ArrayList) qiyaInfoDao.queryBuilder()
                    .where(QiyaInfoDao.Properties.Datetime.ge(thisday))
                    .orderAsc(QiyaInfoDao.Properties.Id)
                    .build().list();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void doDeleteAllQiyaInfo() {
        if (qiyaInfoList == null || qiyaInfoList.size() < 1) {
            return;
        }

        QiyaInfoDao qiyaInfoDao = AppContext.getInstances().getDaoSession().getQiyaInfoDao();
        qiyaInfoDao.deleteAll();
        qiyaInfoList.clear();
    }


    public QiyaInfo getQiyaInfoById(long id) {
        if (qiyaInfoList == null) {
            return null;
        }

        for (QiyaInfo qiyaInfo : qiyaInfoList) {
            if (qiyaInfo.getId() == id) {
                return qiyaInfo;
            }
        }
        return null;
    }

    QiyaInfo _lastQiyaInfo;//一分钟存一次

    public  QiyaInfo _thisQiyaInfo = null;//当前的气压信息

    public Long saveQiyaInfo(QiyaInfo qiyaInfo) {
        if (qiyaInfo == null) {
            return 0L;
        }

        _thisQiyaInfo = qiyaInfo;

        if (_lastQiyaInfo != null) {
            long xTime = qiyaInfo.getDatetime() - _lastQiyaInfo.getDatetime();
            if (xTime < 60000) {
                return 0L;
            }
        }

        long id = 0;
        try {
            QiyaInfoDao qiyaInfoDao = AppContext.getInstances().getDaoSession().getQiyaInfoDao();

            if (qiyaInfo.getId() == null) {
                id = qiyaInfoDao.insert(qiyaInfo);
                _lastQiyaInfo = qiyaInfo;
            } else {
                qiyaInfoDao.update(qiyaInfo);
                id = qiyaInfo.getId();
            }
        } catch (SQLiteException e) {

        }

        return id;
    }

    public ArrayList<QiyaInfo> getQiyaInfoList() {
        return qiyaInfoList;
    }

}
